#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#define BUFFER_SIZE 5
int buffer[BUFFER_SIZE];
int in=0,out=0;
sem_t empty,full;
pthread_mutex_t mutex;

void *producer(void *arg){
        int i;
        for(i=0;i<10;i++){
		sem_wait(&empty);
                pthread_mutex_lock(&mutex);
                buffer[in]=i;
		printf("Produced:%d\n",buffer[in]);
		in=(in+1)%BUFFER_SIZE;
                pthread_mutex_unlock(&mutex);
		sem_post(&full);
        }
	return NULL;
}
void *consumer(void *arg){
        int i;
        for(i=0;i<10;i++){
                sem_wait(&full);
                pthread_mutex_lock(&mutex);
                int data=buffer[out];
                printf("Consumed:%d\n",data);
                out=(out+1)%BUFFER_SIZE;
                pthread_mutex_unlock(&mutex);
                sem_post(&empty);
        }
        return NULL;
}

int main(void){
        pthread_t tid1,tid2;
	sem_init(&empty,0,BUFFER_SIZE);
	sem_init(&full,0,0);
        pthread_mutex_init(&mutex,NULL);
        pthread_create(&tid1,NULL,producer,NULL);
        pthread_create(&tid2,NULL,consumer,NULL);
        pthread_join(tid1,NULL);
        pthread_join(tid2,NULL);
	sem_destroy(&empty);
	sem_destroy(&full);
	pthread_mutex_destroy(&mutex);
	return 0;
}
